package com.neardrug.services.impl;

import com.neardrug.common.Constants;
import com.neardrug.controller.dto.UserDTO;
import com.neardrug.controller.dto.UserLoginDTO;
import com.neardrug.controller.dto.UserResetDTO;
import com.neardrug.entity.User;

import com.neardrug.exception.ServiceException;
import com.neardrug.mapper.UserMapper;
import com.neardrug.services.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    /**
     * 用户注册
     * @param userDTO
     */
    @Override
    public void save(UserDTO userDTO) {
        //邮箱不可重复
        String email=userDTO.getEmail();
        List<User> userList=userMapper.judgeByEmail(email);
        if (userList!=null && userList.size()>0){
            throw new ServiceException(Constants.ALREADY_EXISTS,"邮箱已存在");
        }

        //输入的两次密码必须一致
        String confirmedPassword = userDTO.getConfirmedPassword();
        String password=userDTO.getPassword();
        if (!password.equals(confirmedPassword)){
            throw new ServiceException(Constants.NOT_EQUAL,"两次输入密码不一致");
        }

        //正常流程
        User user=new User();
        BeanUtils.copyProperties(userDTO,user);
        //设置加密密码
        String originPassword=userDTO.getPassword();
        user.setPassword(DigestUtils.md5DigestAsHex(originPassword.getBytes()));
        userMapper.save(user);
    }

    /**
     * 用户登陆
     * @param userLoginDTO
     * @return
     */
    @Override
    public User login(UserLoginDTO userLoginDTO) {
        String email= userLoginDTO.getEmail();//获取邮箱
        String password= userLoginDTO.getPassword();//获取明文密码

        //根据邮箱查询user
        User user=userMapper.getByEmail(email);
        if (user==null){//用户不存在
            throw new ServiceException(Constants.USER_NOT_EXISTS,"用户不存在");
        }

        //现在能确保用户存在，判断密码是否正确
        password=DigestUtils.md5DigestAsHex(password.getBytes());
        if (!password.equals(user.getPassword())){//密码错误
            throw new ServiceException(Constants.PASSWORD_ERROR,"密码错误");
        }

        //校验均正确，返回该user对象
        return user;
    }

    /**
     * 用户忘记密码重设密码
     * @param userResetDTO
     */
    @Override
    public void reset(UserResetDTO userResetDTO) {
        //首先根据输入的名字和邮箱判断数据库有没有这个用户
        User user=userMapper.judgeByEmailAndName(userResetDTO);
        if (user==null){//若没有查出响应的user，说明姓名或邮箱有误
            throw new ServiceException(Constants.USER_NOT_EXISTS,"该姓名及邮箱对应的账号不存在");
        }
        //判断重设密码的两次密码输入是否正确
        String password=userResetDTO.getPassword();
        String confirmedPassword = userResetDTO.getConfirmedPassword();
        if (!password.equals(confirmedPassword)){//若两次输入的密码不一致
            throw new ServiceException(Constants.NOT_EQUAL,"两次输入密码不一致");
        }
        //更新数据库中该用户的密码
        password=DigestUtils.md5DigestAsHex(password.getBytes());
        user.setPassword(password);//在这个user中更新密码
        userMapper.update(user);
    }
}
